/**
 * 开始界面
 * 如果有与现有不一样的需求自己拓展实现
 */

import GameCtr from "../../Controller/GameCtr";
import AudioManager from "../../Common/AudioManager";
import GameData from "../../Common/GameData";
import vv from "../../Common/vv";
import GameDataInstance from "../../Common/GameDataInstance";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Start extends cc.Component {

    @property(cc.Node)
    ndStart: cc.Node = null;
    @property(cc.Node)
    ndShareTip: cc.Node = null;

    @property(cc.Node)
    ndSlider: cc.Node = null;

    @property(cc.Node)
    ndSlidermoregame: cc.Node = null;

    @property(cc.Node)
    ndcloseAudio: cc.Node = null;//

    @property(cc.Node)
    ndmaxsocre: cc.Node = null;//

    @property(cc.Node)
    ndloading: cc.Node = null;

    @property(cc.Label)
    ndtip: cc.Label = null;

    private sliderData = null;//浮窗
    private sliderDataGame = null;//更多游戏


    //排行榜
    @property(cc.Node)
    rankingView:cc.Node = null
    // LIFE-CYCLE CALLBACKS:
    isMusic = false;
    onLoad() {
        // if (!GameCtr.isonceRad) {
        //     GameCtr.isonceRad = true;
        //    // this.scheduleOnce(this.clickRed, 1);
        // }
        // else {
        //     GameCtr.getCashBase();
        // }
        // WXCtr.setBannerAd(WXCtr.advid);
        // // this.schedule(() => {
        // //     WXCtr.setBannerAd(WXCtr.advid);
        // // }, 10);
        // // let dddd = Math.random().toString(32).substr(2);
        // //  console.log("onLoadonLoadonLoadonLoad", dddd);
        //GameData.getAllGameData();

        // WXCtr.getStorageData("score", (data) => {
        //     if (data) {
        //         GameData.score = data;
        //         this.ndmaxsocre.getComponent(cc.Label).string = "" + GameData.score;
        //         console.log("scorescorescorescore");
        //     }
        // });

        if (GameCtr.ins == null) {
            this.ndloading.active = true;
        }
        GameCtr.getInstance().setStart(this);
    }

    onDestroy() {

    }

    start() {

      

        if(GameDataInstance.Instance.openid==null)
        {
            console.log('第13次登陆')
            if(tt)
            {
                tt.login({
                    success: function (res) {
                        console.log('code数据',res.code)

                        vv.sendPostHttpRequest(GameDataInstance.Instance.getopenidUrl,{'code':res.code},(res)=>
                        {
                            console.log('换取openid',res)
                            if(res.status == '1'||res.status == 1)
                            {
                                console.log('走了这里没?')
                              //  let data = JSON.parse(res.data)
                                GameDataInstance.Instance.openid = res.data.openid
                               // cc.sys.localStorage.setItem('openid',GameDataInstance.Instance.openid)
                                console.log('请求的openid',GameDataInstance.Instance.openid)

                                //检查用户是否存在

                                vv.sendPostHttpRequest(GameDataInstance.Instance.hasopenid,{"openid": GameDataInstance.Instance.openid},(res)=>
                                {
                                    console.log('检查用户是否存在',res)
                                      if(res.msg != 'openid不存在')
                                      {
                                         console.log('用户已经注册')
                                       
                                      }
                                      else
                                      {
                                          console.log('用户不存在重新注册')
                                          window['wx'].getUserInfo({
                                            success: (res) => {
                                                let userInfo = res.userInfo;
                                                let nickName = userInfo.nickName;
                                                let avatarUrl = userInfo.avatarUrl;
                                                let gender = userInfo.gender; //性别 0：未知、1：男、2：女
                                                let province = userInfo.province;
                                                let city = userInfo.city;
                                                let country = userInfo.country;
                                                let data = "openid=" + GameDataInstance.Instance.openid  + "&gender=" + gender + "&avatar=" + avatarUrl + "&nickname=" + nickName;
                                                vv.sendPostHttpRequest(GameDataInstance.Instance.updatememberUrl, data, (text) => {
                                                    if (text.status == "1"||text.status == 1) {
                                                        console.log('用户注册成功',text);

                                                    }
                                                });
                                            }
                                        });
                                      }
                                })
                          
                            }
                        })
                    }
                  })
            }
        }


        this.ndStart.on('touchend',()=>
        {
            cc.director.loadScene('fkxxl')
        })


        this.rankingView.on('touchend',()=>
        {
           
            vv.sendPostHttpRequest('https://sjh5.88youxi.cn/app/index.php?i=55&c=entry&do=paihangbang&m=wxs_api',{'city':0,'openid':GameDataInstance.Instance.openid,'page_size':10,'page':1},(res)=>
            {
                console.log('排行榜',res)
              // console.log('排行list22222',res.data)
               console.log('data',res.data.list)
             //  GameDataInstance.Instance.paihangData = res.data
              // let paihanglist = JSON.parse(res.data)
            //   console.log('排行list',res.data)
             //  console.log('2222',res.data)
                vv.openPrefab('Lgpaihang',(node)=>
                {
                    GameDataInstance.Instance.mypaihangdata = res.data.my
                    let rankingnode = node.getComponent('LGRankingview').requestui(res.data.list)
                })
           })
        })
        // GameCtr.getSliderAndMoreGameKAIGUAN("186");               //获取开始界面广告配置
        // GameCtr.getSliderAndMoreGameKAIGUAN("187");               //获取开始界面广告配置
        // GameCtr.getSliderConfig("184");
        // GameCtr.getSliderConfig("185");
        //WXCtr.getFriendRankingData();                   //获取好友排行榜数据
        this.ndShareTip.active = GameCtr.shareSwitch;
        let testUrl = cc.url.raw("resources/texture/start/btn_start.png")
    }

    //开始游戏
    startGame() {
        GameCtr.startGame();
    }

    // //按钮点击事件
    // clickBtn(event, data) {
    //     switch (data) {
    //         case "group":
    //             WXCtr.getGroupRankingData();
    //             break;
    //         case "invite":
    //             WXCtr.share();
    //             break;
    //         case "share":
    //             WXCtr.share();
    //             break;
    //         case "ranking":
    //             GameCtr.showRanking("Start");
    //             break;
    //         case "closeAduio":
    //             AudioManager.getInstance().musicOn = this.isMusic;
    //             AudioManager.getInstance().soundOn = this.isMusic;
    //             this.isMusic = !this.isMusic;
    //             this.ndcloseAudio.getComponent(cc.Toggle).isChecked = this.isMusic;
    //             break;
    //         case "yaoqing":
    //             WXCtr.shareInvite();
    //             break;
    //         case "daliySignin":
    //             GameCtr.daliySignin();
    //             break;
    //     }
    // }

    // setqiandao(isqiandaoguol) {
    //     if (isqiandaoguol) {
    //         for (let index = 0; index < array.length; index++) {
    //             const element = array[index];

    //         }
    //     } else {
    //         this.ndtip.string = "今天已经签到过来"
    //     }
    // }

    //显示广告按钮
    showSlideKAIGUAN(sliderData, slideType) {
        if (slideType == "186") {
            if (sliderData.data.kg == "1") {
                this.ndSlider.active = true;
            }
            else {
                this.ndSlider.active = false;
            }
        }
        else if (slideType == "187") {
            console.log("showSlideKAIGUANshowSlideKAIGUAN", sliderData.data.kg);
            if (sliderData.data.kg == "1") {
                this.ndSlidermoregame.active = true;
            }
            else {
                this.ndSlidermoregame.active = false;
            }
        }
    }

    //显示广告按钮
    showSlide(sliderData) {
        GameCtr.sliderData = sliderData;
        let spr = this.ndSlider.getComponent(cc.Sprite);
        console.log("showSlide", GameCtr.sliderData);
        GameCtr.loadImg(spr, GameCtr.sliderData[0].thumb);
    }

    //显示广告按钮
    showmoreGaem(sliderData) {
        GameCtr.sliderDataGame = sliderData;
        console.log("showmoreGaem", GameCtr.sliderDataGame);
        // this.ndSlidermoregame.active = true;
        let spr = this.ndSlidermoregame.getComponent(cc.Sprite);
        GameCtr.loadImg(spr, GameCtr.sliderDataGame[0].thumb);
    }

    // //点击广告按钮
    // clickSlider(event, data) {
    //     let imgUrl = GameCtr.sliderData[parseInt(data)].img;
    //     WXCtr.previewImg(imgUrl);
    // }

    aaaa() {
        GameCtr.score = 2001;
        GameCtr.sendScore();
    }

    update(dt) {

    }

    // /**
    // * 更多游戏
    // */
    // btnsliderD() {
    //     console.log("GameCtr.otherData = ", GameCtr.sliderData);
    //     if (GameCtr.sliderData)
    //         WXCtr.gotoOther(GameCtr.sliderData[0]);
    // }

    // /**
    // * 更多游戏
    // */
    // btnMoreGame() {
    //     console.log("GameCtr.otherData = ", GameCtr.sliderDataGame);
    //     if (GameCtr.sliderDataGame)
    //         WXCtr.gotoOther(GameCtr.sliderDataGame[0]);
    // }

    // openCustomService() {
    //     WXCtr.customService();
    // }


    // inviteFriends() {
    //     // let call = (data) => {
    //     //     for (let index = 0; index < data.length; index++) {
    //     //         let element = data[index];
    //     //         let ndoe = cc.instantiate(this.ndfreiendCell);
    //     //         this.friendcontent.addChild(ndoe);
    //     //     }
    //     // }
    //     WXCtr.shareInvite();
    // }

    // showFriend() {
    //     WXCtr.hideBannerAd();
    //     this.ndfriend.active = true;
    //     let call = (res) => {
    //         this.ndfriend.getComponent(friendlist).setdata(res);
    //     }
    //     GameCtr.getInviteFriend(call.bind(this));
    // }

    // closeFriend() {
    //     this.ndfriend.active = false;
    //     WXCtr.showBannerAd();
    // }

    /**
    *看视频接着玩
    */
    // lockMoiveGoGame() {
    //     if (window.wx != undefined) {
    //         WXCtr.showVideoAd();
    //     }
    // }

    // lookMovie() {
    //     if (window.wx != undefined) {
    //         WXCtr.showVideoAd();
    //     }
    // }
}
